package com.stoneframe.service.manageAuthorize;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.stoneframe.service.BaseService;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.ProviderHelper;
import com.stoneframe.util.baseArray.ArrayHelper;

@Service(value="sysAuthorizeDataService")
public class SysAuthorizeDataService extends BaseService {

	protected SysAuthorizeDataService() {
		super("SysAuthorizeDataMapper");
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 获得权限范围用户ID
	 * */
	public String getDataAuthorUserId() throws Exception{
		if(ProviderHelper.isSystem()){ //是否是管理员
			//return (List<PageData>)dao.findForList("SysModuleMapper.listAll", null);
		}
		//非管理员
		PageData currentUserData=ProviderHelper.getCurrentUser();
		String userId=currentUserData.getString("userId");
		List<PageData> authorList=(List<PageData>)dao.findForList(mapper+".listAll", userId);
		StringBuilder userSB=new StringBuilder("select userId from sys_user where 1=1");
		userSB.append(" and (userId ='"+userId+"'");
		for(PageData authorData :authorList){
			int authorizeType=Integer.parseInt(authorData.get("authorizeType").toString());
			switch(authorizeType){
			case 0: //0代表最大权限
				return "";
			case -2: //本人及下属
				userSB.append(String.format(" or managerId ='%s'", userId));
				break;
			case -3:
				userSB.append(" or departmentId=(select departmentId from sys_user"+
                              "where userId ='"+userId+"')");
				break;
			case -4:
				userSB.append(" or organizeId=(select organizeId from sys_user"+
                              "where userId ='"+userId+"')");
                break;
            case -5:
            	userSB.append(" or departmentId='"+authorData.getString("resourceId")+"' OR organizeId='"+authorData.getString("resourceId")+"'");
                break;
				
			}
		}
		userSB.append(")");
		List<String> userIdList=new ArrayList();
		List<PageData> userList=(List<PageData>)dao.findForList("DynamicExcuteSqlMapper.excuteSql", new PageData("sql", userSB.toString()));
		if(null!=userList){
			for(PageData userData:userList){
				userIdList.add(userData.getString("userId"));
			}
		}
		String result= ArrayHelper.listToString(userIdList,',');
		return result;
		
	}

}
